<?php
// comments soon (tm)
class qConfiguration
{
    public function __construct()
    {
    }
    
    public function __destruct()
    {
        foreach ($this->elements as $elem)
        {
            unset($elem);
        }
    }
    
    public function Insert($key, $value)
    {
        if (count($key) == 0) return false;
        
        if (count($key) == 1 && $key[0] == -1)
        {
            $this->elements[] = $value;
            return true;
        }
        
        else if (count($key) == 1 && $value instanceof qConfiguration)
        {
            if (!isset($this->elements[$key[0]]))
            {
                this->elements[$key[0]] = $value;
                return true;
            }
            
            else if (!($this->elements[$key[0]] instanceof qConfiguration))
            {
                return false;
            }
            
            foreach ($value->Values as $key => $element)
            {
                $this->elements[$key[0]]->Insert(array($key), $element);
            }
        }
        
        else if (count($key) == 1)
        {
            $this->elements[$key[0]] = $value;
            return true;
        }
        
        else
        {
            if (!isset($this->elements[$key[0]]))
            {
                $this->elements[$key[0]] = new qConfiguration;
            }
            
            else if (!($this->elements[$key[0]] instanceof qConfiguration))
            {
                return false;
            }
            
            $array = array();
            for ($i = 1; $i < count($key); $i++)
            {
                $array[] = $key[$i];
            }
            $this->elements[$key[0]]->Insert($array, $value);
        }
        
        return true;
    }
    
    private $elements = array();
}